﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Pointandclick.Objects;
using Pointandclick.Animation;

namespace Pointandclick.ScreenManagement
{
    class TestingScreen : BaseScreen
    {
        #region Properties

        /// <summary>
        /// Player used for testing
        /// </summary>
        Player testPlayer;

        /// <summary>
        /// List of triggers that interact with the inventory items
        /// </summary>
        List<Trigger> triggerList;

        /// <summary>
        /// List of pickable items
        /// </summary>
        List<Pickable> pickList;

        MouseInteractive exit;

        /// <summary>
        /// A hash that contains the inventory textures according to its keys
        /// </summary>
        Hashtable textureHash;

        Texture2D spriteSheet;
        Texture2D green;
        Texture2D white;
        Texture2D yellow;
        Texture2D red;

        AnimatedSprite gohan;

        #endregion

        /// <summary>
        /// This is a testing screen that demo's the newest mechanics
        /// </summary>
        /// <param name="manager">the screen manager which it belongs to</param>
        public TestingScreen(ScreenManager manager)
        {
            this.Manager = manager;
            LoadContent();
            pickList = new List<Pickable>();
            triggerList = new List<Trigger>();
            textureHash = new Hashtable();
            testPlayer = new Player(white, new Point(0, manager.Window.ClientBounds.Height - white.Height));
            exit = new MouseInteractive(red, new Point(400, 300));
            exit.Rectangle = new Rectangle(exit.Rectangle.X,exit.Rectangle.Y,50,50);
            textureHash.Add("test1", red);
            textureHash.Add("test2", white);
            textureHash.Add("test3", green);
            textureHash.Add("test4", yellow);
            gohan = new AnimatedSprite(spriteSheet, 3, 12, Direction.Horizontal);
            gohan.PlayOnce();

            //Populates de pickable list
            for (int i = 1; i <= textureHash.Count; i++)
            {
                pickList.Add(new Pickable((Texture2D)textureHash["test" + i], new Point(100 * i, 100), "test" + i));
                triggerList.Add(new Trigger(new Rectangle(100 * (i - 1) + 40, 40, 50, 50), "test" + i));
            }
        }


        /// <summary>
        /// Load the graphic content necessary for this screen
        /// </summary>
        /// <param name="Content"></param>
        protected override void LoadContent()
        {
            white = Manager.Content.Load<Texture2D>("whiteRectangle");
            green = Manager.Content.Load<Texture2D>("greenRectangle");
            yellow = Manager.Content.Load<Texture2D>("yellowRectangle");
            red = Manager.Content.Load<Texture2D>("redRectangle");
            Background = Manager.Content.Load<Texture2D>("background");
            spriteSheet = Manager.Content.Load<Texture2D>("gohan");
        }

        /// <summary>
        /// Updates the screen
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (Inputs.Keyboard.IsKeyDown(Keys.Escape))
                Manager.ChangeScreen(new MainMenuScreen(Manager));
            testPlayer.Update(triggerList, Inputs.Mouse);
            for (int i = 0; i < pickList.Count; i++)
            {
                string key = pickList[i].Update();
                if (key != "")
                {
                    pickList.RemoveAt(i);
                    testPlayer.Pickup(key, (Texture2D)textureHash[key]);
                }
            }
            if (exit.ClickedOn())
            {
                Manager.PopUp(new MainMenuPopUp(Manager));
            }
            gohan.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch, Color color)
        {
            spriteBatch.Draw(Background, new Vector2(0, 0), color);
            for (int i=0; i<triggerList.Count;i++)
            {
                spriteBatch.Draw((Texture2D)textureHash[triggerList[i].Key],triggerList[i].Rectangle, color);
            }
            exit.Draw(spriteBatch, color);
            foreach (Pickable pickable in pickList)
                pickable.Draw(spriteBatch, color);
            testPlayer.Draw(spriteBatch, color);
            gohan.Draw(spriteBatch, new Vector2(300, 300), color);
        }
    }
}
